package com.histone.heartbeat.web;

import com.histone.heartbeat.pojo.UrlEntity;
import com.histone.heartbeat.pojo.resp.ResultBean;
import com.histone.heartbeat.service.UrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("url")
@Slf4j
public class UrlController {
    @Autowired
    private UrlService urlService;

    @PostMapping
    public String addUrl(@RequestBody UrlEntity urlEntity) {
        urlEntity.setCreateDate(LocalDate.now());
        urlEntity.setLevel(3);
        return urlService.saveUrl(urlEntity) == null ? "fail" : "ok";
    }

    @GetMapping
    public ResultBean getUrls(String userid) {
        List<UrlEntity> urls = urlService.getUrlsByUserid(userid);
        return new ResultBean<List>(urls);
    }

    @DeleteMapping("{id}")
    public ResultBean delUrl(@PathVariable String id) {
        urlService.delUrl(id);
        return new ResultBean<String>("success");

    }
}
